//
//  MHMusicCatergoryTableViewController.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/10/12.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHMusicCatergoryTableViewController.h"
#import "TCBGMCell.h"
#import "TCBGMHelper.h"
#import "MHCustomItemViewModel.h"
#import "MHMusicCatergoryViewModel.h"
#import "TCBGMHelper.h"
#import "MHAVAudioManager.h"
@interface MHMusicCatergoryTableViewController ()<TCBGMCellDelegate>
@property(nonatomic, strong) MHMusicCatergoryViewModel *viewModel;
@property(nonatomic, strong) TCBGMCell *currentCell;
@property(nonatomic, strong) TCBGMElement *currentElement;
@end

@implementation MHMusicCatergoryTableViewController
@dynamic viewModel;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView mh_registerNibCell:TCBGMCell.class];
    self.tableView.estimatedRowHeight= 85;
    self.tableView.rowHeight=UITableViewAutomaticDimension;
    self.tableView.emptyDataSetDelegate = nil;
    self.tableView.emptyDataSetSource = nil;
}


- (UITableViewCell *)tableView:(UITableView *)tableView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath{
    return [tableView dequeueReusableCellWithIdentifier:@"TCBGMCell"];
}


-(void)searchBarTextShouldReturn:(JKRSearchBar *)searchBar
{
    if (searchBar.text.length > 0) {
        [self.view endEditing:YES];
        self.viewModel.searchText = searchBar.text;
        [searchBar resignFirstResponder];
        [self.viewModel.requestRemoteDataCommand execute:@(1)];
        self.tableView.emptyDataSetDelegate = self;
        self.tableView.emptyDataSetSource = self;
    }
}


-(UIEdgeInsets)contentInset
{
    if (self.viewModel.isSearch) {
        return UIEdgeInsetsMake(0 , 0, 0, 0);
    }else
    {
        return [super contentInset];
    }
    
}

- (void)onBGMDownLoad:(TCBGMCell *)cell;
{
    
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    TCBGMElement* ele =  self.viewModel.datas[indexPath.row];
    [AXAppDelegate sharedDelegate].currentSong = ele;
    NSString *cacheName = [NSString stringWithFormat:@"%@.%@",[ele title],[[ele fullSongUrl] pathExtension]];
    NSString *cachePath = [NSHomeDirectory() stringByAppendingPathComponent: [NSString stringWithFormat:@"Documents/bgm/%@", cacheName]];
    if([[NSFileManager defaultManager] fileExistsAtPath:[NSHomeDirectory() stringByAppendingPathComponent:cachePath]]){
        //            [self goBack];
        [MHNotificationCenter postNotificationName:MHVideoLoadMusicNotification object:nil userInfo:@{@"patch":[NSHomeDirectory() stringByAppendingPathComponent:cachePath]}];
    }
    else [[TCBGMHelper sharedInstance] downloadBGM: ele];
}

- (void)configureCell:(TCBGMCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(MHCustomItemViewModel *)customItem{
    TCBGMElement *ele = customItem.model;
    [cell bindViewModel:ele];
    cell.delegate = self;
    cell.timeLabel.text = ele.timeLength;
    cell.authorLabel.text = ele.artists;
    cell.isLocal = ele.dataFromLocal;
    if ([ele.fullSongUrl isEqualToString:self.currentElement.fullSongUrl] && [MHAVAudioManager shareManager].isPlaying) {
        cell.playBtn.selected = YES;
    }else
    {
        cell.playBtn.selected = NO;
    }
    //    cell.playBtn.selected = [MHAVAudioManager shareManager].isPlaying;
    //    cell.playBtn.hidden =  ![ele.fullSongUrl isEqualToString:self.currentElement.fullSongUrl] ;
    if (!ele.dataFromLocal) {
        [cell.musicIcon yy_setImageWithURL:[NSURL URLWithString:ele.fullCoverUrl] placeholder:[UIImage imageNamed:@"img_music_default"]];
    }else
    {
        cell.musicIcon.image = ele.image;
    }
    if ([ele isCacheInLocal]) {
        [cell setDownloadProgress:1.0];
        cell.progressView.hidden = NO;
    }else{
        cell.progressView.hidden = YES;
        [cell.downLoadBtn setTitle: @"使用" forState:UIControlStateNormal];
    }
    
    cell.musicLabel.text = ele.title;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [super tableView:tableView didSelectRowAtIndexPath:indexPath];
    TCBGMCell *cell =  [tableView cellForRowAtIndexPath:indexPath];
    self.currentElement = self.viewModel.datas[indexPath.row];
    self.currentCell = cell;
    
    //    [AXAppDelegate sharedDelegate].currentSong = self.currentElement;
    
    @weakify(self);
    //    [RACObserve([MHAVAudioManager shareManager], isPrepare) subscribeNext:^(NSNumber *num) {
    //        @strongify(self);
    //        self.currentCell.playBtn.hidden =  ![num boolValue];
    //    }];
    [RACObserve([MHAVAudioManager shareManager], isPlaying) subscribeNext:^(NSNumber *num) {
        @strongify(self);
        self.currentCell.playBtn.selected =  [num boolValue];
    }];
    [self.tableView reloadData];
}

-(void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    //    [[MHAVAudioManager shareManager].player replaceCurrentItemWithPlayerItem:nil];
    [[MHAVAudioManager shareManager] pause];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
